<template>
  <div class="weather-container">
    <!-- 搜索框 -->
    <div class="search-section">
      <div class="search-input-wrapper">
        <input 
          type="text" 
          v-model="city"
          placeholder="请输入城市名称"
        >
        <button class="search-button" @click="searchWeather">
          <el-icon><Search /></el-icon>
        </button>
      </div>
    </div>

    <!-- 天气信息 -->
    <div class="weather-content">
      <div class="weather-main">
        <h2>{{ city2 }}天气</h2>
        <div class="weather-icon">
          <img :src="`/src/assets/images/${weather.icon}.svg`" alt="天气图标">
        </div>
        <div class="weather-temp">
          <span class="temp-value">{{ weather.temp }}°</span>
          <span class="temp-text">{{ weather.text }}</span>
        </div>
      </div>

      <!-- 详细信息 -->
      <div class="weather-details">
        <el-row class="details-row">
          <el-col :span="6">
            <div class="detail-item">
              <span class="value">{{ weather.feelsLike }}°</span>
              <span class="label">体感温度</span>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="detail-item">
              <span class="value">{{ weather.windScale }}级</span>
              <span class="label">{{ weather.windDir }}</span>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="detail-item">
              <span class="value">{{ weather.humidity }}%</span>
              <span class="label">相对湿度</span>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="detail-item">
              <span class="value">{{ weather.windSpeed }}km/h</span>
              <span class="label">风速</span>
            </div>
          </el-col>
        </el-row>

        <el-row class="details-row">
          <el-col :span="6">
            <div class="detail-item">
              <span class="value">{{ weather.wind360 }}°</span>
              <span class="label">风向角度</span>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="detail-item">
              <span class="value">{{ weather.pressure }}hPa</span>
              <span class="label">气压</span>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="detail-item">
              <span class="value">{{ weather.vis }}km</span>
              <span class="label">能见度</span>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="detail-item">
              <span class="value">{{ weather.cloud }}%</span>
              <span class="label">云量</span>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import axios from 'axios'

const API_KEY = '94f66cc8c6c04c1f8d9b66db1079d662'
const city = ref('')
const city2 = ref('郑州')
const weather = ref({} as any)
const location = ref('101180101')

onMounted(() => {
  fetchWeather()
})

const fetchWeather = async () => {
  try {
    const res = await axios.get("https://devapi.qweather.com/v7/weather/now", {
      params: {
        location: location.value,
        key: API_KEY
      }
    })
    weather.value = res.data.now
  } catch (error) {
    console.error('获取天气信息失败:', error)
  }
}

const searchWeather = async () => {
  try {
    city2.value = city.value
    // 获取城市ID
    const locationRes = await axios.get("https://geoapi.qweather.com/v2/city/lookup", {
      params: {
        key: API_KEY,
        location: city.value
      }
    })
    
    location.value = locationRes.data.location[0].id
    await fetchWeather()
  } catch (error) {
    console.error('搜索天气信息失败:', error)
  }
}
</script>

<style scoped>
.weather-container {
  width: 100%;
}

.search-section {
  background-color: rgb(27, 160, 160);
  padding: 15px;
}

.search-input-wrapper {
  display: flex;
  align-items: center;
  background: #fff;
  border-radius: 20px;
  padding: 5px 15px;
}

.search-input-wrapper input {
  flex: 1;
  border: none;
  outline: none;
  padding: 8px;
  font-size: 14px;
}

.search-button {
  background: none;
  border: none;
  color: rgb(10, 223, 238);
  cursor: pointer;
  padding: 8px;
}

.weather-content {
  background-color: rgb(7, 107, 138);
  padding: 20px;
  color: #fff;
}

.weather-main {
  text-align: center;
  margin-bottom: 30px;
}

.weather-icon img {
  width: 100px;
  height: 100px;
  margin: 20px 0;
}

.weather-temp {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
}

.temp-value {
  font-size: 35px;
  font-weight: bold;
}

.temp-text {
  font-size: 25px;
}

.weather-details {
  background-color: rgb(7, 24, 177);
  border-radius: 10px;
  padding: 20px;
}

.details-row {
  margin-bottom: 20px;
}

.detail-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
}

.detail-item .value {
  font-size: 16px;
  font-weight: bold;
}

.detail-item .label {
  font-size: 14px;
  opacity: 0.8;
}
</style>